package com.rx.mgt.service;

import com.rx.mgt.dao.EmailListDao;
import com.rx.mgt.dao.EmailListMemberDao;
import com.rx.mgt.dao.UserInfoExtDao;
import com.rx.mgt.domain.EmailListDomain;
import com.rx.mgt.domain.EmailListMember;
import com.rx.mgt.domain.UserInfoExt;
import com.rx.mgt.po.UsersBo;
import com.rx.mgt.util.StringUtil;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * Created by zxj on 2017/5/25.
 */
@Service
public class EmailListService {
    @Autowired
    private EmailListDao emailListDao;
    @Autowired
    UserInfoExtDao userInfoExtDao;
    @Autowired
    private EmailListMemberDao emailListMemberDao;
    @Transactional
    public List<EmailListDomain> queryEmailList(UsersBo usersBo) {
       return emailListDao.findByDomain(usersBo);
    }

    @Transactional(readOnly = false,propagation = Propagation.REQUIRED)
    public void saveEmailList(EmailListDomain model) {
        emailListDao.save(model);
    }
    @Transactional(readOnly = false,propagation = Propagation.REQUIRED)
    public List<EmailListMember> queryEmailMemberListByEmailListId(String id) {
       return emailListMemberDao.getEmailListMembersByEmaiListId(id);
    }
    @Transactional
    public EmailListDomain queryEmailListDomain(String id) {
        return emailListDao.get(Long.valueOf(id));
    }

    @Transactional(readOnly = false,propagation = Propagation.REQUIRED)
    public void updateEmailListDomain(EmailListDomain model) {
        if(StringUtils.isBlank(model.getIndertDate())){
            model.setIndertDate(new SimpleDateFormat("yyyy-MM-dd").format(new Date()));
        }
        emailListDao.saveOrUpdate(model);
    }

    @Transactional(readOnly = false,propagation = Propagation.REQUIRED)
    public void deleteEmailListById(String id) {
        EmailListDomain domain = emailListDao.get(Long.valueOf(id));
        emailListDao.delete(domain);

    }
    @Transactional
    public UserInfoExt getUserByDomainAndEmail(String domian, String email) {
        return userInfoExtDao.getUserByDomainAndEmail(domian,email);
    }

    @Transactional(readOnly = false,propagation = Propagation.REQUIRED)
    public void addEmailToEmailListMember(EmailListMember member) {
        emailListMemberDao.save(member);
    }

    @Transactional
    public EmailListMember getEmailListMemberByEmail(String email) {
        String hql = " from EmailListMember e where 1=1 and e.email =:email";
        return  emailListMemberDao.getOne(hql,email);
    }

    @Transactional
    public EmailListMember queryEmailListMemberById(String id) {
        return emailListMemberDao.get(Long.valueOf(id));
    }

    @Transactional(readOnly = false,propagation = Propagation.REQUIRED)
    public void updateEmailListMember(EmailListMember member) {
        emailListMemberDao.saveOrUpdate(member);
    }

    @Transactional(readOnly = false,propagation = Propagation.REQUIRED)
    public void deleteEmailListMemberById(String id) {
        EmailListMember member = emailListMemberDao.get(Long.valueOf(id));
        emailListMemberDao.delete(member);

    }
}
